import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import {createSvgIconsPlugin} from 'vite-plugin-svg-icons'
import { viteMockServe } from 'vite-plugin-mock'
import { ConfigEnv, UserConfigExport, loadEnv } from 'vite'
// https://vite.dev/config/
export default defineConfig(
    ({command, mode}: ConfigEnv): UserConfigExport => {
        // 获取各种环境下对应的变量
        let env = loadEnv(mode, process.cwd())
        return {
            plugins: [
                vue(),
                viteMockServe({
                    mockPath: 'mock', // mock文件夹路径
                    enable: command === 'serve' // 只有开发环境才开启mock
                }),
                createSvgIconsPlugin({
                    // 指定 SVG 图标目录
                    iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
                    // 指定symbolId格式
                    symbolId: 'icon-[dir]-[name]'
                })
            ],
            resolve: {
                alias: {
                    "@": path.resolve("./src") // 相对路径别名配置
                }
            },
            css: {
                preprocessorOptions: {
                    scss: {
                        javascriptEnabled: true,
                        additionalData: '@use "@/assets/styles/variable.scss";',
                    },
                },
            },
            // 代理跨域
            server: {
                proxy: {
                    [env.VITE_APP_BASE_API]: {
                        target: env.VITE_SERVE,
                        // 需要代理跨域
                        changeOrigin: true,
                        rewrite: (path) => path.replace(/^\/api/, ''),
                    },
                },
            },
        }
    })

